package com.eims.sls_app.ui.adapter;

import java.util.List;

import org.json.JSONObject;

import com.android.volley.VolleyError;
import com.eims.sls_app.dataaccess.Lottery;
import com.eims.sls_app.ui.MainActivity;
import com.eims.sls_app.utils.App;
import com.eims.sls_app.utils.AppTools;
import com.eims.sls_app.utils.RequestUtil;
import com.eims.sls_app.view.CustomDigitalClock;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import bls.juejin.cn.R;

public class GridView_HallLotteryAdapter extends BaseAdapter {
	private int holdPosition;
	private boolean isChanged = false;
	private boolean ShowItem = false;

	private final static String TAG = "GridView_HallLotteryAdapter";
	// 上下文本
	private Context context;
	// 装cp的集合
	private List<Lottery> listLotterys;
	private MainActivity activity;
	private int mHidePosition = -1;

	public GridView_HallLotteryAdapter(Context context,
			List<Lottery> listLotterys) {
		this.context = context;
		activity = (MainActivity) context;
		this.listLotterys = listLotterys;
	}

	@Override
	public int getCount() {
		if (0 == listLotterys.size() % 2) {
			return listLotterys.size();
		} else {
			return listLotterys.size() + 1;
		}
	}

	@Override
	public Lottery getItem(int position) {
		// if (0 == listLotterys.size() % 2) {
		return listLotterys.get(position);
		// } else {
		// return listLotterys.get(position-1);
		// }
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	public void showDropItem(boolean showItem) {
		this.ShowItem = showItem;
	}

	@Override
	public View getView(int position, View view, ViewGroup parent) {
		final ViewHolder holder;
		// 判断View是否为空
		if (view == null) {
			holder = new ViewHolder();
			LayoutInflater inflater = LayoutInflater.from(context);
			// 得到布局文件
			view = inflater.inflate(R.layout.gridview_item_hall, null);
			// 得到控件
			holder.iv_lottery = (ImageView) view.findViewById(R.id.iv_lottery);
			holder.iv_lottery_kj = (ImageView) view
					.findViewById(R.id.iv_lottery_kj);
			holder.tv_hall_lotteryname = (TextView) view
					.findViewById(R.id.tv_hall_lotteryname);

			// holder.tv_hall_describ = (TextView) view
			// .findViewById(R.id.tv_hall_describ);
			holder.cdc_hall_time = (CustomDigitalClock) view
					.findViewById(R.id.cdc_hall_time);
			holder.tv_hall_dm = (TextView) view.findViewById(R.id.tv_hall_dm);
			holder.iv_hall_jiajiang = (TextView) view
					.findViewById(R.id.iv_hall_jiajiang);
			holder.tvStopSale = (TextView) view.findViewById(R.id.tv_stopSale);
			view.setTag(holder);
		} else {
			holder = (ViewHolder) view.getTag();
		}

		// holder.tv_hall_describ.setVisibility(View.VISIBLE); // 彩种描述隐藏掉
		holder.iv_lottery_kj.setVisibility(View.GONE);
		holder.iv_hall_jiajiang.setVisibility(View.GONE);
		holder.iv_lottery.setVisibility(View.VISIBLE);
		holder.tvStopSale.setVisibility(View.GONE);
		final Lottery lottery = listLotterys.get(position != listLotterys
				.size() ? position : 0);
		if (0 == listLotterys.size() % 2) { // 判断当彩种为双数
			setParams(lottery, holder);
		} else {
			if (listLotterys.size() != position) { // 为单数时最后一个设置要区分
				setParams(lottery, holder);
			} else {
				holder.cdc_hall_time.setVisibility(View.GONE);
				holder.tv_hall_lotteryname.setText("");
				holder.iv_lottery
						.setBackgroundResource(R.drawable.selector_hall_gvitem);
				// holder.tvStopSale.setVisibility(View.GONE); 提到外面加载
				// 玩法数量为单数时的gridview
				// holder.tv_hall_describ.setText("");
				holder.tv_hall_dm.setVisibility(View.GONE);
				holder.iv_lottery.setVisibility(View.GONE);
			}
		}

		if (isChanged) {
			if (position == holdPosition) {
				if (!ShowItem) {
					view.setVisibility(View.INVISIBLE);
				}
			}
		}
		return view;
	}

	private void setParams(final Lottery lottery, final ViewHolder holder) {
		// // 给控件赋值
		holder.cdc_hall_time.setVisibility(View.GONE);
		holder.tv_hall_lotteryname.setText(lottery.getLotteryName());
		holder.iv_lottery.setImageResource(App.allLotteryLogo.get(lottery
				.getLotteryID()));
		if (lottery.getAddaward() != null
				&& lottery.getAddaward().equals("True")) {
			holder.iv_hall_jiajiang.setVisibility(View.VISIBLE);
		}
		// 是否显示今日开奖
		if (App.winToday.contains(lottery.getLotteryID())) {
			holder.iv_lottery_kj.setVisibility(View.VISIBLE);
		}
		if (lottery.getEndtime() == null)
			holder.cdc_hall_time.setVisibility(View.GONE);

		// holder.tv_hall_describ.setText(lottery.getLotteryDescription());
		// 判断是否有期号 或 是足球是由有对阵
		if ("72".equals(lottery.getLotteryID())
				|| "73".equals(lottery.getLotteryID())
				|| "45".equals(lottery.getLotteryID())) {
			if ("true".equals(lottery.getIsSale())) {
				holder.cdc_hall_time.setVisibility(View.GONE);
				holder.tvStopSale.setVisibility(View.GONE); // 让显示停止销售的布局消失
				if ("73".equals(lottery.getLotteryID())) {
					holder.tv_hall_dm.setText(lottery.getDtmatch());
				}
				holder.tv_hall_dm.setVisibility(View.VISIBLE);

			} else if ("false".equals(lottery.getIsSale())) {

				holder.cdc_hall_time.setVisibility(View.GONE);
				holder.tvStopSale.setVisibility(View.VISIBLE);
				holder.tv_hall_dm.setVisibility(View.GONE);
			}
			// holder.tv_hall_dm.setVisibility(View.VISIBLE);
			// holder.tv_hall_dm.setText(lottery.getLotteryAgainst());
		} else {
			if ("true".equals(lottery.getIsSale())) {
				if ("0".equals(lottery.getIsuseId())) {
					holder.cdc_hall_time.setVisibility(View.GONE);
				} else {
					holder.cdc_hall_time.setVisibility(View.VISIBLE);
				}
				holder.tvStopSale.setVisibility(View.GONE); // 让显示停止销售的布局消失

			} else if ("false".equals(lottery.getIsSale())) {
				holder.cdc_hall_time.setVisibility(View.GONE);
				holder.tvStopSale.setVisibility(View.VISIBLE);
			}
			if (lottery.getEndtime() != null
					&& lottery.getDistanceTime() - System.currentTimeMillis() > 0) {
				holder.cdc_hall_time.setMTickStop(false);
				holder.cdc_hall_time.setType(1);
				holder.cdc_hall_time.setEndTime(lottery.getDistanceTime());
			} else {
				if (lottery.getDistanceTime2() - System.currentTimeMillis() > 0) {
					holder.cdc_hall_time.setMTickStop(false);
					holder.cdc_hall_time.setType(2);
					holder.cdc_hall_time.setEndTime(lottery.getDistanceTime2());
				}
			}
			holder.tv_hall_dm.setVisibility(View.GONE);
			if ("100".equals(lottery.getLotteryID())) {
				holder.tv_hall_lotteryname.setText("更多内容");
				holder.cdc_hall_time.setVisibility(View.GONE);
				holder.tvStopSale.setVisibility(View.GONE);
				holder.tv_hall_dm.setVisibility(View.VISIBLE);
				holder.tv_hall_dm.setText("敬请期待");
			}

		}
		// 倒计时
		holder.cdc_hall_time
				.setClockListener(new CustomDigitalClock.ClockListener() {
					@Override
					public void timeEnd() {
						if (holder.cdc_hall_time.getType() == 1) {
							holder.cdc_hall_time.setMTickStop(false);
							holder.cdc_hall_time.setType(2);
							holder.cdc_hall_time.setEndTime(lottery
									.getDistanceTime2());
						} else {
							if ("72".equals(lottery.getLotteryID())
									|| "73".equals(lottery.getLotteryID())
									|| "45".equals(lottery.getLotteryID())) {
								return;
							}
							RequestUtil requestUtil = new RequestUtil(context,
									false, 0) {
								@Override
								public void responseCallback(
										JSONObject reponseJson) {
									String result = AppTools
											.getDate(reponseJson);
									if ("0".equals(result)) {
										activity.update();
									} else if ("-1001".equals(result)) {
										if (RequestUtil.DEBUG) {
											Log.e(TAG, "获取购彩大厅数据出错");
										}
									}
								}

								@Override
								public void responseError(VolleyError error) {
								}
							};
							requestUtil.getLotteryData(lottery.getLotteryID());
						}
					}

					@Override
					public void remainFiveMinutes() {
					}
				});
	}

	/**
	 * 静态类
	 */
	static class ViewHolder {
		ImageView iv_lottery, iv_lottery_kj; // 彩种图片
		TextView tv_hall_lotteryname; // 彩种名
		// TextView tv_hall_describ;// 彩种描述
		CustomDigitalClock cdc_hall_time; // 截止时间
		TextView tv_hall_dm; // 最近比赛
		TextView iv_hall_jiajiang; // 加奖

		TextView tvStopSale;
	}
	// public void exchange(int startPosition, int endPosition) {
	// holdPosition = endPosition;
	// Lottery startObject = getItem(startPosition);
	//
	// if(startPosition < endPosition){
	//
	// listLotterys.add(endPosition + 1, (Lottery) startObject);
	// listLotterys.remove(startPosition);
	// }else{
	// listLotterys.add(endPosition,(Lottery)startObject);
	// listLotterys.remove(startPosition + 1);
	// }
	// isChanged = true;
	// this.notifyDataSetChanged();
	// }

	// /** 静态类 */
	// static class ViewHolder {
	// ImageView iv_lottery; // 彩种图片
	// TextView tv_hall_lotteryname; // 彩种名
	// TextView tv_hall_describ;// 彩种描述
	// CustomDigitalClock cdc_hall_time; // 截止时间
	// TextView tv_hall_dm; // 最近比赛
	// TextView iv_hall_jiajiang; // 加奖
	// }

	// @Override
	// public void reorderItems(int oldPosition, int newPosition) {
	// Lottery temp1 = listLotterys.get(oldPosition);
	//
	// if(oldPosition < newPosition){
	// for(int i=oldPosition; i<newPosition; i++){
	// Collections.swap(listLotterys, i, i+1);
	// }
	// }else if(oldPosition > newPosition){
	// for(int i=oldPosition; i>newPosition; i--){
	// Collections.swap(listLotterys, i, i-1);
	// }
	// }
	// listLotterys.set(newPosition, temp1);
	// }
	//
	// @Override
	// public void setHideItem(int hidePosition) {
	// this.mHidePosition = hidePosition;
	// notifyDataSetChanged();
	// }
}
